﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<FinancialManagement.Models.DepBudgetCollection>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    <script type="text/javascript">
        //用于两个Grid关联
        var projectID;
        var errorMsg = "操作失败！";
        var successMsg = "操作成功！";
        function onRowSelected(e) {
            var detailsGrid = $('#Details').data('tGrid');
            projectID = e.row.cells[0].innerHTML;
            // update ui text        
            $('#projectID').text(projectID);
            // rebind the related grid        
            detailsGrid.rebind();

            $('#btnAdd').attr("style", "visibility:visible");
        }

        function onDataBinding(e) {
            e.data = $.extend(e.data, { projectID: projectID });
        }

        //添加按钮事件
        function addContent() {
            var request = $.ajax({
                url: "../Budget/ShowAddWindow",
                data: { id: projectID },
                type: "POST", dataType: "html"
            });
            request.done(function (msg) {
                $("#Window").data("tWindow").center().open();
                $("#tContent").html(msg);
            });
            request.fail(function (jqXHR, textStatus) {
                alert("Request failed: " + textStatus);
            });
        }

        //ShowAddWindow.ascx 中提交按钮的事件
        function insertContent() {
            var request = $.ajax({
                url: "../Budget/InsertDepBudgetProjectDetail",
                data: $("#tContent form").serializeArray(),
                type: "POST", dataType: "text"

            });
            request.done(function (msg) {
                if (msg == "success") {
                    alert(successMsg);
                    $("#Window").data("tWindow").close();
                    $("#Details").data("tGrid").rebind();
                    $("#tContent").html("");
                }
                else {
                    alert(errorMsg);
                }
            });
            request.fail(function (jqXHR, textStatus) {
                alert("Request failed: " + textStatus);
            });
        }

        //修改按钮事件
        function editContent(dsid) {
            var request = $.ajax({
                url: "../Budget/ShowEditWindow",
                data: { id: dsid },
                type: "POST", dataType: "html"
            });
            request.done(function (msg) {
                $("#Window").data("tWindow").center().open();
                $("#tContent").html(msg);
            });
            request.fail(function (jqXHR, textStatus) {
                alert("Request failed: " + textStatus);
            });
        }

        //ShowEditWindow.ascx 中取消按钮的事件
        function closeWindow() {
            $("#Window").data("tWindow").close();
            $("#tContent").html("");
        }

        //ShowEditWindow.ascx 中更新按钮的事件
        function saveContent() {
            var request = $.ajax({
                url: "../Budget/SaveDepBudgetProjectDetail",
                data: $("#tContent form").serializeArray(),
                type: "POST", dataType: "text"

            });
            request.done(function (msg) {
                if (msg == "success") {
                    alert(successMsg);
                    $("#Window").data("tWindow").close();
                    $("#Details").data("tGrid").rebind();
                    $("#tContent").html("");
                }
                else {
                    alert(errorMsg);
                }
            });
            request.fail(function (jqXHR, textStatus) {
                alert("Request failed: " + textStatus);
            });

        }

        //删除按钮事件
        function deleteContent(dsid) {
            var request = $.ajax({
                url: "../Budget/DeleteDepBudgetProjectDetail",
                data: { id: dsid },
                type: "POST", dataType: "text"

            });
            request.done(function (msg) {
                if (msg == "success") {
                    alert(successMsg);
                    $('#Details').data('tGrid').rebind();
                }
                else {
                    alert(errorMsg);
                }

            });
            request.fail(function (jqXHR, textStatus) {
                alert("Request failed: " + textStatus);
            });
        }
    </script>
    <title>预算管理</title>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        预算管理</h2>
    <h3>
        单位名称：<%= ViewBag.DepName %></h3>
    <%--用于加载项目预算明细信息的Window--%>
    <% Html.Telerik().Window()
                .Name("Window")
                .Title("项目预算明细信息")
                .Draggable(true)
                .Resizable(resizing => resizing
                    .Enabled(true)
                    )
                    .Scrollable(true)
                    .Modal(true)
                    .Buttons(b => b.Close())
                    .Content(() =>
                    {
    %>
    <div id="tContent">
    </div>
    <%
                    }
                  )
                 .Width(370)
                 .Height(600)
                 .Visible(false)
                 .Render();   
    %>
    <h3>
        项目信息</h3>
    <%= Html.Telerik().Grid<FinancialManagement.Models.DepBudgetCollection>()
        .Name("Project")
        .Columns(columns =>
        {            
            columns.Bound(o => o.ProjectID).Width(200);
            columns.Bound(o => o.ProjectName).Width(200);
            columns.Bound(o => o.TotalBudget).Width(100);
            columns.Bound(o => o.Notes);
        })
        .DataBinding(dataBinding =>
                dataBinding.Ajax().Select("SelectionClientSide_ProjectTables", "Budget"))
        .Pageable()
        .Sortable()
        .Selectable()
        .ClientEvents(events => events.OnRowSelect("onRowSelected"))        
        .RowAction(row => row.Selected = row.DataItem.ProjectID.Equals(ViewData["id"]))       
    %>
    <h3>
        项目预算明细信息 (项目编号：<span id="projectID"><%= ViewData["id"] %></span>)
    </h3>
    <%= Html.Telerik().Grid<FinancialManagement.Models.ProjectTbaleDetails>()        
            .Name("Details")
                    .ToolBar(toolbar => toolbar.Template("<input type='button' id='btnAdd' style='visibility:hidden' value='添加' onclick='addContent()'/>"))
            .DataKeys(keys => { keys.Add(o => o.DSID); })
            .DataBinding(dataBinding => {
                dataBinding.Ajax().Select("_ClientEditTemplate", "Budget"); 
            })
            .Columns(columns=>        
            {            
                columns.Bound(c => c.DSID).Width(100);
                columns.Bound(c => c.ProjectID).Width(200);
                columns.Bound(c => c.ProjectBudgetName);
                columns.Bound(c => c.ProjectBudgetMoney).Width(200);
                columns.Bound(c => c.Annual).Width(200);
                columns.Bound(c => c.BudgetAccding).Width(200);
                columns.Bound(c => c.Illustration).ClientTemplate("<a href='<#= Illustration #>'>下载</a>").Width(200);
                columns.Bound(c => c.Notes).Width(200);
                columns.Bound(o => o.ProjectID).ClientTemplate("<input type='button' id='btnEdit' value='修改' onclick='editContent(<#= DSID #>)'/> <input type='button' id='btnDelete' value='删除' onclick='deleteContent(<#= DSID #>)'/>").Title("操作");
            })        
            .DataBinding(dataBinding => dataBinding.Ajax().Select("SelectionClientSide_ProjectDetails",
                                        "Budget", new { projectID = "" }))        
            .ClientEvents(clientEvents => clientEvents.OnDataBinding("onDataBinding"))        
            .Pageable()        
            .Sortable()        
    %>
</asp:Content>
